package com.ruoyi.web.controller.template;


import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.CustomerChat;
import com.ruoyi.system.domain.SysPost;
import com.ruoyi.system.domain.TemplateChat;
import com.ruoyi.system.service.ITemplateChatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/template/chat")
public class TemplateChatController extends BaseController {

    @Autowired
    private ITemplateChatService templateChatService;

    @PreAuthorize("@ss.hasPermi('template:chat:list')")
    @GetMapping("/list")
    public TableDataInfo list(TemplateChat templateChat) {
        startPage();
        LoginUser loginUser = getLoginUser();
        SysUser sysUser =loginUser.getUser();
        if (sysUser.getUserName().equals("A001")){
            templateChat.setTemplateId(103);
        }
        List<TemplateChat> list = templateChatService.selectChatList(templateChat);
        return getDataTable(list);
    }
    

    @Log(title = "模板管理", businessType = BusinessType.UPDATE)
    @PutMapping("/updateChatStatus")
    public AjaxResult updateChatStatus(@RequestBody TemplateChat templateChat) {
        LoginUser loginUser = getLoginUser();
        SysUser sysUser =loginUser.getUser();
        if (sysUser.getUserName().equals("A001")){
            templateChat.setTemplateId(103);
        }
        return toAjax(templateChatService.updateTemplateChat(templateChat));
    }


    @Log(title = "模板导出", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('template:chat:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, TemplateChat user) {
        List<TemplateChat> list = templateChatService.selectChatList(user);
        ExcelUtil<TemplateChat> util = new ExcelUtil<TemplateChat>(TemplateChat.class);
        util.exportExcel(response, list, "模板数据");
    }


    @Log(title = "模板导入", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('template:chat:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<TemplateChat> util = new ExcelUtil<TemplateChat>(TemplateChat.class);
        List<TemplateChat> userList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = templateChatService.importTemplate(userList, updateSupport, operName);
        return success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<TemplateChat> util = new ExcelUtil<TemplateChat>(TemplateChat.class);
        util.importTemplateExcel(response, "模板数据");
    }

    /**
     * 新增模板
     */
    @PreAuthorize("@ss.hasPermi('template:chat:add')")
    @Log(title = "模板管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody TemplateChat post) {
        LoginUser loginUser = getLoginUser();
        SysUser sysUser =loginUser.getUser();
        if (sysUser.getUserName().equals("A001")){
            post.setTemplateId(103);
        }
        return toAjax(templateChatService.insertTemplateChat(post));
    }

    /**
     * 修改模板
     */
    @PreAuthorize("@ss.hasPermi('template:chat:edit')")
    @Log(title = "模板管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody TemplateChat post) {
        LoginUser loginUser = getLoginUser();
//        SysUser sysUser =loginUser.getUser();
//        if (sysUser.getUserName().equals("A001")){
//            post.setTemplateId(103);
//        }
        return toAjax(templateChatService.updateTemplateChat(post));
    }

    /**
     * 根据模板编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('template:chat:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {

        return success(templateChatService.selectChatById(id));
    }

    /**
     * 删除模板
     */
    @PreAuthorize("@ss.hasPermi('template:chat:remove')")
    @Log(title = "模板管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
//        for(int i=0;i<ids.length;i++){
//            templateChatService.selectChatById(ids[i]);
//        }
        return success(1);
    }
}
